﻿(function ($) {

    $.util.namespace("window.extlib.common");

    var current = window.extlib.common;

    var apiJsonPath = "/Common/api/api.json";
    var apiPathPrefix = "/Common/api/";

    current.methods = {

        getApiJson: function (targetModule, targetExt, callbackForDefaults, callbackForMethods) {
            $.get(apiJsonPath, function (data) {
                var the = {};
                for (var i = 0; i < data.length; i++) {
                    var module = data[i].module;
                    if (module != targetModule) { continue; }
                    var extensions = data[i].extensions;
                    for (var k = 0; k < extensions.length; k++) {
                        var ext = extensions[k].extension;
                        if (ext != targetExt) { continue; }
                        the = extensions[k];
                        break;
                    }
                    if (!$.isEmptyObject(the)) { break; }
                }
                if ($.isEmptyObject(the)) { return; }
                $.get(apiPathPrefix + "/" + targetModule + "/" + the.api, function (data) {
                    if ($.isFunction(callbackForDefaults)) { callbackForDefaults.call(this, data.defaults); }
                    if ($.isFunction(callbackForMethods)) { callbackForMethods.call(this, data.methods); }
                });
            });
        },

        searchApi: function (key, callback) {
            if (!$.util.isString(key)) { return; }
            if ($.string.isNullOrWhiteSpace(key)) { return; }
            $.get(apiJsonPath, function (data) {
                var the = {};
                for (var i = 0; i < data.length; i++) {
                    var module = data[i].module;
                    var extensions = data[i].extensions;
                    for (var k = 0; k < extensions.length; k++) {
                        let ext = extensions[k].extension;
                        let path = apiPathPrefix + "/" + module + "/" + extensions[k].api;
                        $.get(path, function (data) {
                            console.log("完成[" + path +"]搜索...");
                            var result = [];
                            $.array.forEach(data.defaults, function (item) {
                                if (item.tag && $.string.contains(item.tag, key)) {
                                    result.push({ module: module, extension: ext, match: item.name });
                                }
                            });

                            $.array.forEach(data.methods, function (item) {
                                if (item.tag && $.string.contains(item.tag, key)) {
                                    result.push({ module: module, extension: ext, match: item.name });
                                }
                            });

                            if (callback && $.isFunction(callback)) { callback.call(this, result); }
                        });
                    }
                }
            });
        },

        createTableDefaults: function () {
            var table = $("<table />").addClass("tableAPI defaults");
            var tr = $("<tr />").addClass("title");
            $("<td />").html("名称").css("width", "17%").appendTo(tr);
            $("<td />").html("类型").css("width", "8%").appendTo(tr);
            $("<td />").html("描述").appendTo(tr);
            $("<td />").html("默认值").css("width", "7%").appendTo(tr);
            tr.appendTo(table);

            return table;
        },

        createTableMethods: function () {
            var table = $("<table />").addClass("tableAPI methods");
            var tr = $("<tr />").addClass("title");
            $("<td />").html("名称").css("width", "10%").appendTo(tr);
            $("<td />").html("重写").css("width", "5%").appendTo(tr);
            $("<td />").html("参数").css("width", "6%").appendTo(tr);
            $("<td />").html("描述").appendTo(tr);

            tr.appendTo(table);

            return table;
        },

        loadApiDocumentForTable: function (targetModule, targetExt) {
            this.getApiJson(targetModule, targetExt, function (defaults) {
                var table = current.methods.createTableDefaults();
                $.array.forEach(defaults, function (item) {
                    var name = item.name, dataType = item.dataType, defaultValue = item.defaultValue, content = item.content;
                    var tr = $("<tr />");
                    $("<td />").html(name).appendTo(tr);
                    $("<td />").html(dataType).appendTo(tr);
                    $("<td />").html(content).appendTo(tr);
                    $("<td />").html(defaultValue).appendTo(tr);
                    tr.appendTo(table);
                });
                table.insertAfter($("div.ext.defaults"));
            }, function (methods) {
                var table = current.methods.createTableMethods();
                $.array.forEach(methods, function (item) {
                    var name = item.name, cover = item.cover ? "是" : "否",
                        parameterName = item.parameterName, returnInfo = item.return;
                    var content = item.content;

                    var extContent = content.extContent,
                        parameterContent = content.parameterContent,
                        remark = content.remark;

                    var parameterType = content.parameterType;

                    var description = [];
                    description.push("<b>" + extContent + "</b>");
                    if (parameterName) {
                        description.push("该方法参数说明: <b>" + parameterContent + "</b>。其值可以是以下类型:");
                        $.array.forEach(parameterType, function (p) {
                            var pType = p.type, pContent = p.content, pRemark = p.remark;
                            var pDescription = [];
                            description.push("<span style=\"padding-left:45px;\">" + pType + "类型，" + pContent + "</span>");
                            if (pRemark) {
                                description.push(pRemark);
                            }
                        });
                    }
                    
                    if (remark) {
                        description.push("<hr />");
                        description.push(remark);
                    } else { description.push("<hr />"); }
                    description.push("<b>方法返回值</b>: " + returnInfo);

                    var tr = $("<tr />");
                    $("<td />").html(name).appendTo(tr);
                    $("<td />").html(cover).appendTo(tr);
                    $("<td />").html(!parameterName ? "无" : parameterName).appendTo(tr);
                    $("<td />").html(description.join("<br />").replace("<hr /><br />", "<hr />")).appendTo(tr);
                    tr.appendTo(table);
                });
                table.insertAfter($("div.ext.methods"));
            });
        }
    };

    // window.open的简易封装
    // url:目标地址
    // param:传递参数，支持object和string格式
    // size:窗口大小，object格式，属性为width和height
    current.windowOpen = function (url, param, size) {
        if ($.string.isNullOrWhiteSpace(url)) { return; }
        if (typeof param == "object") {
            url = url + "?" + $.param(param);
        } else if (typeof param == "string") {
            url = url + "?" + param;
        }
        if (typeof size == "object") {
            if (size.width == undefined) { size.width = screen.width - 150 }
            if (size.height == undefined) { size.height = screen.height - 150 }
        } else { size = { width: screen.width - 150, height: screen.height - 150 }; }
        var sFeatures = "top=" + ((screen.height - size.height - 50) / 2) + ",left=" + ((screen.width - size.width) / 2) + ",width=" + size.width + ",height=" + size.height + ",location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no";
        var tmp = window.open(url, "", sFeatures);
        //tmp.moveTo((screen.width - size.width) / 2, (screen.height - size.height) / 2);
        //tmp.resizeTo(size.width, size.height);
        //tmp.focus();
        //tmp.location = url;
    };

    current.loadApi = function (targetModule, targetExt) {
        current.methods.loadApiDocumentForTable(targetModule, targetExt);
    };

    current.searchApi = function (key, callback) { current.methods.searchApi(key, callback); };

})(jQuery);